<template>
  <div class="login-container">
    <van-nav-bar
      class="page-nav-bar"
      title="登录"
    >
      <van-icon
        slot="left"
        color="#fff"
        name="cross"
        @click="$router.push('/my')"
      />
    </van-nav-bar>
    <!--
      @submit="onSubmit"
      关于表单验证，
      1，给van-field 按如下规则，或查阅官网，进行rules配置
      2，若规则未通过van-form 里的@submit="onSubmit"事件不会触发
     -->
    <van-form
      ref="formDate"
      @submit="onSubmit"
    >
      <!-- type="number" maxlength="11"
      上面两个皆是h5中方的，限制输入框类型、最大长
       -->
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i
          slot="left-icon"
          class="toutiao toutiao-shouji"
        ></i>
      </van-field>
      <van-field
        v-model="user.code"
        type="password"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
      >
        <i
          slot="left-icon"
          class="toutiao toutiao-yanzhengma"
        ></i>
        <template #button>
          <van-count-down
            v-if="isShowCode"
            millisecond
            :time="time"
            format="ss s"
            @finish="isShowCode = false"
          />
          <!-- native-type 用于控制按钮类型 -->
          <van-button
            v-else
            class="send-sms-btn"
            round
            size="small"
            native-type="button"
            type="default"
            @click="onSendSms"
          >发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button
          class="login-btn"
          block
          type="info"
          native-type="submit"
        >提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, checkCode } from '@/api/user'
export default {
  name: 'loginIndex',
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /1[7|3|5|6|8]\d{9}/, message: '格式错误' }
        ],
        code: [
          { required: true, message: '请填写密码' },
          { pattern: /\d{6}/, message: '格式错误' }
        ]
      },
      time: 60 * 1000,
      isShowCode: false
    }
  },
  methods: {
    async onSubmit () {
      const user = this.user
      // toast 是vant 里的轻提示模块，在vue中要用this.$toast形式使用，轻提示只会存在一个，前面的会被清除掉
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true
      })
      try {
        const res = await login(user)
        console.log(res)
        this.$store.commit('setUser', res.data.data)
        this.$toast.success('登录成功')
        this.$router.push('/my')
        // 或者  登录成功，跳转回原来页面
        // this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('登陆失败')
        } else {
          this.$toast.fail('登陆失败')
        }
      }
    },
    async onSendSms () {
      try {
        await this.$refs.formDate.validate('mobile')
      } catch (err) {
        return console.log('验证失败', err)
      }
      this.isShowCode = true
      try {
        const res = await checkCode(this.user.mobile)
        console.log(res)
        this.$toast.success('发送成功')
      } catch (err) {
        this.isShowCode = false
        if (err.response.status === 429) {
          this.$toast.fail('发送过于频繁，请稍后再试')
        } else if (err.response.status === 404) {
          this.$toast.fail('手机号不正确，请稍后再试')
        } else {
          this.$toast.fail('发送失败，请稍后再试')
        }
      }
    }
  },
  created () { }
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px !important;
  }
}

.send-sms-btn {
  width: 164px;
  height: 46px;
  line-height: 46px;
  font-size: 15px !important;
  color: #666;
  background-color: #ededed;
}

.login-btn-wrap {
  padding: 53px 33px;

  .login-btn {
    background-color: #6db4fb;
    border: none;
  }
}
</style>
